library(tidyverse)
library(phyloseq)
library(speedyseq)
library(plotly)
options(getClass.msg=FALSE) # https://github.com/epurdom/clusterExperiment/issues/66
#this fixes an error message that pops up because the class 'Annotated' is defined in two different packages
'%!in%' <- function(x,y)!('%in%'(x,y))
source("https://raw.githubusercontent.com/fconstancias/DivComAnalyses/master/R/phyloseq_taxa_tests.R")
source("https://raw.githubusercontent.com/fconstancias/DivComAnalyses/master/R/phyloseq_normalisation.R")
## Loading required package: scales
##
## Attaching package: 'scales'
## The following object is masked from 'package:purrr':
##
## discard
## The following object is masked from 'package:readr':
##
## col_factor
## Loading required package: reshape2
##
## Attaching package: 'reshape2'
## The following object is masked from 'package:tidyr':
##
## smiths
source("https://raw.githubusercontent.com/fconstancias/DivComAnalyses/master/R/phyloseq_alpha.R")
source("https://raw.githubusercontent.com/fconstancias/DivComAnalyses/master/R/phyloseq_beta.R")
source("https://raw.githubusercontent.com/fconstancias/DivComAnalyses/master/R/phyloseq_heatmap.R")
ps = "data/processed/physeq_update_23_11.RDS"
ps %>%
here::here() %>%
readRDS() %>%
phyloseq_get_strains_fast() %>%
phyloseq_remove_chloro_mitho() -> physeq
## Joining, by = "ASV"
physeq
## phyloseq-class experiment-level object
## otu_table() OTU Table: [ 346 taxa and 384 samples ]
## sample_data() Sample Data: [ 384 samples by 50 sample variables ]
## tax_table() Taxonomy Table: [ 346 taxa by 8 taxonomic ranks ]
## phy_tree() Phylogenetic Tree: [ 346 tips and 344 internal nodes ]
## refseq() DNAStringSet: [ 346 reference sequences ]
physeq@sam_data %>%
data.frame() %>%
rownames_to_column('id') %>%
left_join(
"data/raw/hplc Fermentation (Salvato automaticamente).xlsx" %>%
readxl::read_xlsx(sheet = "All total"),
by = c("Sample_description" = "Sample_Id")) %>%
column_to_rownames('id') %>%
sample_data() -> physeq@sam_data
physeq
## phyloseq-class experiment-level object
## otu_table() OTU Table: [ 346 taxa and 384 samples ]
## sample_data() Sample Data: [ 384 samples by 63 sample variables ]
## tax_table() Taxonomy Table: [ 346 taxa by 8 taxonomic ranks ]
## phy_tree() Phylogenetic Tree: [ 346 tips and 344 internal nodes ]
## refseq() DNAStringSet: [ 346 reference sequences ]
We will be analyzing only the PolyFermS samples here so take a subset of the physeq object.
physeq %>%
subset_samples(Experiment == "Continuous") %>%
subset_samples(Paul %!in% c("Paul")) %>%
subset_samples(Reactor != "IR2") -> ps_polyFermS
sample_data(ps_polyFermS)$Reactor <- fct_relevel(sample_data(ps_polyFermS)$Reactor, "IR1", "CR", "TR1", "TR2","TR3", "TR4", "TR5", "TR6")
sample_data(ps_polyFermS)$Treatment <- fct_relevel(sample_data(ps_polyFermS)$Treatment, "UNTREATED", "CTX+HV292.1", "CTX","HV292.1","VAN+CCUG59168", "VAN", "CCUG59168")
sample_data(ps_polyFermS)$Reactor_Treatment <- fct_relevel(sample_data(ps_polyFermS)$Reactor_Treatment, "IR1_UNTREATED","CR_UNTREATED", "CR_CTX", "CR_VAN", "TR1_CTX+HV292.1","TR2_CTX", "TR3_HV292.1", "TR5_VAN+CCUG59168", "TR4_VAN", "TR6_CCUG59168")
ps_polyFermS %>%
sample_data() %>%
data.frame() -> df
measures = df %>% select(ends_with("mM")) %>% colnames()
# define a function to plot scatter plot
my_fn <- function(data, mapping, ...){
p <- ggplot(data = data, mapping = mapping) +
geom_point() +
geom_smooth(method=lm, ...)
p
}
df %>%
GGally::ggpairs(columns = measures,
ggplot2::aes(colour = Reactor),
# legend = 1,
progress = FALSE,
upper = list(
continuous = GGally::wrap('cor', method = "pearson")
),
lower = list(continuous = my_fn)) -> p_corr
p_corr
df %>%
plot_alphas(measure = measures,
x_group = "Reactor_Treatment",
colour_group = "Enrichment",
fill_group = "Enrichment",
shape_group = "Enrichment",
facet_group = "Reactor_Treatment",
test_group = "Reactor_Treatment",
test_group_2 = "Enrichment") -> out
plot_alpha_time <- function(df,
x = "Day_from_Inoculum",
y = "value",
shape = "neg",
fill = "Reactor_Treatment",
group = "Reactor_Treatment",
facet)
{
df %>%
arrange(Day_from_Inoculum) %>%
ggplot(aes_string(x = x,
y = y, shape = shape)) +
geom_point(size=2, alpha=0.9, aes_string(group = group, color = fill, fill = fill), show.legend = FALSE) +
geom_path(inherit.aes = TRUE, aes_string(group=group),
size = 0.08,
linetype = "dashed") +
facet_grid(as.formula(facet), scales = "free") +
theme_light() +
scale_color_viridis_d(na.value = "black") +
geom_vline(xintercept = c(23,39),
color="black", alpha=0.4) +
# geom_smooth(show.legend = TRUE, level = 0.95) +
scale_x_continuous(breaks=seq(0,90,10)) -> plot
return(plot)
}
out$plot$data %>%
dplyr::filter(alphadiversiy == "Total_SCFA_mM") %>%
dplyr::mutate(neg = ifelse(value == 0, "neg", "pos")) %>%
arrange(Day_from_Inoculum) %>%
ggplot(aes_string(x = "Day_from_Inoculum",
y = "value", group = "Reactor_Treatment")) +
geom_jitter(size=0.5, alpha=0.9, aes_string(group = "Reactor_Treatment", color = "Reactor_Treatment", fill = "Reactor_Treatment"), show.legend = TRUE) +
geom_path(inherit.aes = TRUE, aes_string(group="Reactor_Treatment", fill = "Reactor_Treatment", color = "Reactor_Treatment", show.legend = FALSE),
size = 0.001,
linetype = "dashed") +
# facet_grid(as.formula(facet), scales = "free") +
geom_vline(xintercept = c(23,39),
color="black", alpha=0.4) +
geom_smooth(show.legend = FALSE, level = 0.95, alpha=0.05, size = 0.5 ,aes_string(group="Reactor_Treatment", color = "Reactor_Treatment", fill = "Reactor_Treatment")) +
scale_x_continuous(breaks=seq(0,90,10)) +
# scale_y_continuous(labels = scientific,
# limits=c(1e+10, 1e+11), breaks = seq(1e+10, 1e+11, by = 1e+10),
# trans = "log10") +
labs(x="Day (from Inoculum)", y= "SCFA concentration [mM]",
col=NULL, fill = NULL, shape = NULL) +
theme_light() +
scale_color_viridis_d(na.value = "black") +
scale_fill_viridis_d(na.value = "black") -> plot
## Warning: Ignoring unknown aesthetics: fill, show.legend
plot + theme(legend.position = "bottom")
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'
plot %>%
plotly::ggplotly()
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'
## Warning: `group_by_()` is deprecated as of dplyr 0.7.0.
## Please use `group_by()` instead.
## See vignette('programming') for more help
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_warnings()` to see where this warning was generated.
plot_time <- function(df,
measure,
x = "Day_from_Inoculum",
y = "value",
shape = "neg",
fill = "Reactor_Treatment",
group = "Reactor_Treatment",
facet)
{
df %>%
dplyr::filter(alphadiversiy %in% measure) %>%
dplyr::mutate(alphadiversiy = fct_reorder(alphadiversiy, value, .desc = TRUE)) %>%
dplyr::mutate(neg = ifelse(value == 0, "neg", "pos")) %>%
arrange(Day_from_Inoculum) %>%
ggplot(aes_string(x = x,
y = y)) +
geom_jitter(size=0.5, alpha=0.9, aes_string(color = fill, fill = fill, shape = shape), show.legend = TRUE) +
geom_path(inherit.aes = TRUE, aes_string(fill = fill, color = fill, show.legend = FALSE),
size = 0.005,
linetype = "dashed") +
facet_grid(as.formula(facet), scales = "free") +
geom_vline(xintercept = c(23,39),
color="black", alpha=0.4) +
geom_smooth(show.legend = FALSE, level = 0.95, alpha=0.05, size = 0.5 ,aes_string(color = fill, fill = fill)) +
scale_x_continuous(breaks=seq(0,90,10)) +
# scale_y_continuous(labels = scientific,
# limits=c(1e+10, 1e+11), breaks = seq(1e+10, 1e+11, by = 1e+10),
# trans = "log10") +
theme_light() +
scale_color_viridis_d(na.value = "black") +
scale_fill_viridis_d(na.value = "black") -> plot
return(plot + theme(legend.position = "bottom"))
}
out$plot$data %>%
plot_time(measure = c("Total_SCFA_mM", "Acetat_mM", "Butyrat_mM", "Propionat_mM", "Isobutyrat_mM", "Valerat_mM", "Isovalerat_mM", "Succinat_mM"),
facet = c("alphadiversiy ~ ."), shape = NULL) +
labs(x="Day (from Inoculum)", y= "SCFA concentration [mM]",
col=NULL, fill = NULL, shape = NULL) +
scale_shape_manual(values=c(4, 19)) -> p4
p4
p4 %>% ggplotly()
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'
htmlwidgets::saveWidget(as_widget(p4 %>% ggplotly()),
paste0(here::here(),
"/data/processed/",
"metabolites_",
format(Sys.time(), "%Y%b%d"),".html"))
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'
# htmlwidgets::saveWidget(as_widget(p4 %>% ggplotly()), paste0("~/Documents/index.html"))
p4 +
facet_null() +
facet_grid(alphadiversiy ~ Reactor_Treatment, scales = "free") +
scale_color_manual(values = rep("black",8)) +
scale_fill_manual(values = rep("black",8))
p4 +
facet_null() +
facet_grid(Reactor_Treatment ~ alphadiversiy, scales = "free") +
scale_color_manual(values = rep("black",8)) +
scale_fill_manual(values = rep("black",8)) +
scale_x_continuous(breaks=seq(0,90,20))
df %>%
dplyr::select(ends_with("mM") | "Total_SCFA_mM") %>%
drop_na() %>%
# t() %>%
scale(center = TRUE,
scale = TRUE) %>%
dist(method= "euc") -> euc_met
plot_ordination(ps_polyFermS,
ordination = phyloseq::ordinate(ps_polyFermS,
distance = euc_met,
method = "PCoA")) -> pca
pca$layers[[1]] = NULL
pca +
geom_point(size=2,
aes(color = Reactor_Treatment,
fill = NULL,
shape = NULL,
alpha = Day_from_Inoculum)) +
theme_light() +
geom_path(arrow = arrow(type = "open", angle = 30, length = unit(0.15, "inches")),
size = 0.08, linetype = "dashed", inherit.aes = TRUE, aes(group=Reactor_Treatment, color = Reactor_Treatment)) +
scale_alpha_continuous(range=c( 0.9, 0.3)) +
scale_color_viridis_d(na.value = "red") +
scale_fill_viridis_d(na.value = "red") +
scale_shape_manual(values = c(8, 21, 22, 23, 24, 16, 15, 18, 17)) +
theme_classic() -> p5
p5
p5 %>%
plotly::ggplotly() -> p5ly
htmlwidgets::saveWidget(as_widget(p5ly),
paste0(here::here(),
"/data/processed/",
"metabolites_",
format(Sys.time(), "%Y%b%d"),"_2.html"))
paste0(here::here(),
"/data/processed/",
"metabolites",
"_",
format(Sys.time(), "%Y%b%d")
,".RData") %>% save.image()
sessionInfo()
## R version 3.6.3 (2020-02-29)
## Platform: x86_64-apple-darwin15.6.0 (64-bit)
## Running under: macOS Mojave 10.14.6
##
## Matrix products: default
## BLAS: /Library/Frameworks/R.framework/Versions/3.6/Resources/lib/libRblas.0.dylib
## LAPACK: /Library/Frameworks/R.framework/Versions/3.6/Resources/lib/libRlapack.dylib
##
## locale:
## [1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
##
## attached base packages:
## [1] stats graphics grDevices utils datasets methods base
##
## other attached packages:
## [1] reshape2_1.4.4 scales_1.1.1 plotly_4.9.2.2 speedyseq_0.3.0
## [5] phyloseq_1.30.0 forcats_0.5.0 stringr_1.4.0 dplyr_1.0.2
## [9] purrr_0.3.4 readr_1.4.0 tidyr_1.1.2 tibble_3.0.4
## [13] ggplot2_3.3.3 tidyverse_1.3.0
##
## loaded via a namespace (and not attached):
## [1] colorspace_2.0-0 ggsignif_0.6.0 ellipsis_0.3.1
## [4] rio_0.5.16 rprojroot_2.0.2 XVector_0.26.0
## [7] fs_1.5.0 rstudioapi_0.13 ggpubr_0.4.0
## [10] farver_2.0.3 fansi_0.4.1 lubridate_1.7.9.2
## [13] xml2_1.3.2 codetools_0.2-18 splines_3.6.3
## [16] knitr_1.30 ade4_1.7-16 jsonlite_1.7.2
## [19] broom_0.7.3 cluster_2.1.0 dbplyr_2.0.0
## [22] compiler_3.6.3 httr_1.4.2 backports_1.2.1
## [25] assertthat_0.2.1 Matrix_1.3-0 lazyeval_0.2.2
## [28] cli_2.2.0 htmltools_0.5.0 prettyunits_1.1.1
## [31] tools_3.6.3 igraph_1.2.6 gtable_0.3.0
## [34] glue_1.4.2 Rcpp_1.0.5 carData_3.0-4
## [37] Biobase_2.46.0 cellranger_1.1.0 vctrs_0.3.6
## [40] Biostrings_2.54.0 multtest_2.42.0 ape_5.4-1
## [43] nlme_3.1-151 crosstalk_1.1.0.1 iterators_1.0.13
## [46] xfun_0.19 openxlsx_4.2.3 rvest_0.3.6
## [49] lifecycle_0.2.0 rstatix_0.6.0 zlibbioc_1.32.0
## [52] MASS_7.3-53 hms_0.5.3 parallel_3.6.3
## [55] biomformat_1.14.0 rhdf5_2.30.1 RColorBrewer_1.1-2
## [58] yaml_2.2.1 curl_4.3 reshape_0.8.8
## [61] stringi_1.5.3 S4Vectors_0.24.4 foreach_1.5.1
## [64] permute_0.9-5 BiocGenerics_0.32.0 zip_2.1.1
## [67] rlang_0.4.10 pkgconfig_2.0.3 evaluate_0.14
## [70] lattice_0.20-41 Rhdf5lib_1.8.0 htmlwidgets_1.5.3
## [73] labeling_0.4.2 tidyselect_1.1.0 here_1.0.1
## [76] GGally_2.1.0 plyr_1.8.6 magrittr_2.0.1
## [79] R6_2.5.0 IRanges_2.20.2 generics_0.1.0
## [82] DBI_1.1.0 pillar_1.4.7 haven_2.3.1
## [85] foreign_0.8-75 withr_2.3.0 mgcv_1.8-33
## [88] abind_1.4-5 survival_3.2-7 modelr_0.1.8
## [91] crayon_1.3.4 car_3.0-10 rmarkdown_2.6
## [94] progress_1.2.2 grid_3.6.3 readxl_1.3.1
## [97] data.table_1.13.6 vegan_2.5-7 reprex_0.3.0
## [100] digest_0.6.27 stats4_3.6.3 munsell_0.5.0
## [103] viridisLite_0.3.0